using MultiAgentsShared;
using static MultiAgentsClient.Manager.AgentManager;

namespace MultiAgentsClient.Presentation;

public partial record AgentHeaderPanelModel
{
    private readonly EventBus eventBus;

    public IState<string> AgentName => State.Value(this, () => "");

    public record SaveAgentClick(string AgentName);

    public AgentHeaderPanelModel()
    {
        eventBus = EventBus.Instance;

        eventBus.Subscribe<LoadAgentCompleteCommand>(async cmd => { await LoadAgentCompleteCommandHandler(cmd); });
    }

    public async Task LoadAgentCompleteCommandHandler(LoadAgentCompleteCommand cmd)
    {
        AgentTemplate agent = cmd.Agent;

        await AgentName.SetAsync(agent.Name);
    }

    public async Task SaveAgent()
    {
        string? agentName = await AgentName;
        if (agentName == null)
        {
            return;
        }
        eventBus.Publish(new SaveAgentClick(agentName));
    }
}
